// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// c [author] rhyme_lph
// r [email]  rhymelph@gmail.com
// o [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_white_button.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/employee_item_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/duplicate_as_new_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';

import 'employee/new_employee_page.dart';

@RouterMeta(paramName: "duplicateAsNewPage")
class DuplicateAsNewPage extends StatefulWidget {
  @override
  _DuplicateAsNewPageState createState() => _DuplicateAsNewPageState();
}

class _DuplicateAsNewPageState extends State<DuplicateAsNewPage>
    with PagePresenter<DuplicateAsNewPage, DuplicateAsNewProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.duplicate_as_new,
      );

  @override
  Widget get buildWidget => Stack(
        children: [
          ColoredBox(
            color: Colors.white,
            child: Column(
              children: [
                AppSpaces.h25,
                SearchInputField(
                  controller: provider.searchController,
                  focusNode: provider.searchFocusNode,
                ),
                AppSpaces.h20,
                Expanded(
                  child: listBuilder<String>(
                    selector: (_, __) => __.searchController.text,
                    builder:
                        (BuildContext context, String value, Widget child) =>
                            CustomScrollView(
                      slivers: buildSearchWidget(value),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Positioned(
              bottom: getDp(32) + MediaQuery.of(context).padding.bottom,
              left: 0.0,
              right: 0.0,
              child: _buildBottomBar()),
        ],
      );

  Widget _buildBottomBar() => Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          NormalFloatTextWhiteButton(
            title: S.current.cancel,
            onTap: provider.onCancel,
          ),
          SizedBox(
            width: getDp(15),
          ),
          NormalFloatTextButton(
            title: S.current.duplicate,
            onTap: provider.onDuplicate,
          ),
        ],
      );

  List<Widget> buildSearchWidget(String value) {
    return provider.groupMap.entries
        .toList()
        .map((e) => buildSliver(e.key, e.value))
        .toList();
  }

  Widget buildSliver(String title, List<EmployeeContactEntity> data) =>
      SliverStickyHeader(
        header: buildSliverHeader(title: title),
        sliver: SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, i) => buildSliverItem(data, i),
            childCount: data.length,
          ),
        ),
      );

  Widget buildSliverItem(List<EmployeeContactEntity> data, int i) {
    return listBuilder<EmployeeContactEntity>(
      selector: (_, __) => __.currentSelect,
      builder:
          (BuildContext context, EmployeeContactEntity value, Widget child) =>
              PressedOpacityButton(
        onPressed: () => provider.onSelectEmployee(data[i]),
        child: EmployeeItemRadio(
          title: data[i].name,
          content: data[i].title,
          isSelect: value == data[i],
        ),
      ),
    );
  }

  Widget buildSliverHeader({String title}) {
    final String _title = title.isEmpty ? "*" : title;
    return ListHeaderItem(
      title: _title,
    );
  }
}
